/// use LineChart
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

var data = [
  new LinearSales(0, 5),
  new LinearSales(1, 25),
  new LinearSales(2, 100),
  new LinearSales(3, 75),
];

var data2 = [
  new LinearSales(0, 25),
  new LinearSales(1, 55),
  new LinearSales(2, 99),
  new LinearSales(3, 75),
];

void main() {
  runApp(MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.fallback(),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Chart Demo'),
        ),
        body: PointsLineChart(),
      )
  ));
}

class PointsLineChart extends StatefulWidget {
  @override
  PointsLineChartState createState() => PointsLineChartState();
}

class PointsLineChartState extends State<PointsLineChart> {

  List<LinearSales> tsdata = data;

  Widget chartWidget() {
    var series = [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (LinearSales sales, _) => sales.year,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: tsdata,
      )
    ];

    var chart = new charts.LineChart(series,
        animate: true,
        animationDuration: const Duration(milliseconds: 500),
        defaultRenderer: new charts.LineRendererConfig(includePoints: true),
    );

    return Container(
      height: 350,
      child: new Card(child: GestureDetector(
        onScaleUpdate: (ScaleUpdateDetails scaleDetails) {
          debugPrint("scale details:" + scaleDetails.toString());
        },
        child: chart,
      )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(
          children: [
            new Expanded(
              child: chartWidget(),
            ),
          ],
        ),
        SizedBox(
          height: 35.0,
        ),
        RaisedButton(
          onPressed: () {
            setState(() {
              tsdata = data2;
            });
          },
          child: Text('Change'),
        ),
      ],
    );
  }
}

/// Sample linear data type.
class LinearSales {
  final int year;
  final int sales;

  LinearSales(this.year, this.sales);
}
